<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'
  export default {
    name: 'SalaryPieChart',
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    data() {
      return {
        salaryData: [],
        chart: null
      };
    },
    watch: {
      salaryData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.getSalaryYearPie();
      this.$nextTick(() => {
        this.initChart();
      });
    },
    methods: {
      getSalaryYearPie() {
        this.getRequest('/salary/analyze/salary-year-pie').then(resp => {
          if (resp) {
            this.salaryData = resp.data;
            this.initChart();
          }
        })
      },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.salaryData);
      },
      setOptions(data = []) {
        
        let legend = [];
        let seriesData = [];
        data.forEach(e => {
          legend.push(e.year + '');
          seriesData.push({
            value: e.sumSalary,
            name: e.year
          });
        });

        this.chart.clear();
        this.chart.setOption({
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)',
            textStyle: {
              color: '#fff'
            }
          },
          legend: {
            left: 'center',
            bottom: '10',
            data: legend
          },
          series: [
            {
              name: '工资年度统计',
              type: 'pie',
              roseType: 'radius',
              radius: [15, 95],
              center: ['50%', '38%'],
              data: seriesData,
              animationEasing: 'cubicInOut',
              animationDuration: 2600
            }
          ]
        })
      }
    }
  }
</script>

<style lang='scss' scoped>

</style>